package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.mappers.UserInfoMapper;
import com.tanhua.model.domain.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@DubboService
public class UserInfoImpl implements UserInfoApi {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long userID) {
        return userInfoMapper.selectById(userID);
    }

    @Override
    public Map<Long, UserInfo> findByIds(List<Long> userIds, UserInfo Info) {
        QueryWrapper wr = new QueryWrapper();
        //用户id列表
        wr.in("id",userIds);
        //添加筛选条件
        if (Info != null){
            if (Info.getAge()!=null){
                wr.lt("age",Info.getAge());
            }
            if (!StringUtils.isEmpty(Info.getGender())){
                wr.eq("gender",Info.getGender());
            }
            if (!StringUtils.isEmpty(Info.getNickname())){
                wr.like("nickname",Info.getNickname());
            }
        }
        List<UserInfo> list = userInfoMapper.selectList(wr);
        //借助id属性来转换
        Map<Long, UserInfo> map = CollUtil.fieldValueMap(list, "id");
        return map;
    }

    //分页查询用户
    @Override
    public IPage findAll(Integer page, Integer pagesize) {
        Page<UserInfo> userInfoPage = userInfoMapper.selectPage(new Page<UserInfo>(page, pagesize), null);
        return userInfoPage;
    }
}
